package views;

import views.panels.PanelGeneral;
import java.awt.BorderLayout;
import logicBusiness.AreaBL;
import models.Area;
import javax.swing.JOptionPane;

/**
 *
 * @author JARR
 */
public class InterfazCrearArea extends javax.swing.JFrame {

    /** Creates new form InterfazCrearArea */
    public InterfazCrearArea() {
        initComponents();
        PanelGeneral panel = new PanelGeneral();
        this.add(panel, BorderLayout.SOUTH);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        CTNombreAreaNueva = new javax.swing.JTextField();
        BCrearArea = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        CTDescripcionArea = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        CTSalidaArea = new javax.swing.JTextField();

        setResizable(false);

        jLabel1.setText("Nombre del área nueva:");

        CTNombreAreaNueva.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CTNombreAreaNuevaActionPerformed(evt);
            }
        });

        BCrearArea.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        BCrearArea.setText("Crear Area");
        BCrearArea.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BCrearAreaActionPerformed(evt);
            }
        });

        jLabel2.setText("Para asignar nuevas asignaturas al area debe crearlas y asignarlas alli");

        jLabel3.setText("Descripción:");

        CTSalidaArea.setEditable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel1))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 287, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(CTNombreAreaNueva)
                                    .addComponent(CTDescripcionArea, javax.swing.GroupLayout.DEFAULT_SIZE, 308, Short.MAX_VALUE))))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(107, 107, 107)
                                .addComponent(BCrearArea, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 249, Short.MAX_VALUE)
                                .addComponent(CTSalidaArea, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(43, 43, 43))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addContainerGap(368, Short.MAX_VALUE))))))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel1, jLabel3});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(CTNombreAreaNueva, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(CTDescripcionArea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BCrearArea)
                    .addComponent(CTSalidaArea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addGap(27, 27, 27))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BCrearAreaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BCrearAreaActionPerformed
        // TODO add your handling code here:
        int ok = 0;
        AreaBL bo = new AreaBL();
        Area tst;
        if (CTNombreAreaNueva.getText().equals("") || CTDescripcionArea.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Debe digitar los datos completos para crear un Area", null, JOptionPane.ERROR_MESSAGE);
        } else {
            tst = bo.consultarArea(CTNombreAreaNueva.getText() + "");

            if (tst == null) {
                ok = bo.crearArea(CTNombreAreaNueva.getText(), CTDescripcionArea.getText());
                CTNombreAreaNueva.setText("");
                CTDescripcionArea.setText("");//mar
            }
            if (ok == 1) {
                JOptionPane.showMessageDialog(this, "La creacion del area fue exitosa", "Creacion exitosa !!!", JOptionPane.INFORMATION_MESSAGE);
            } else {
                if (tst != null) {
                    JOptionPane.showMessageDialog(this, "Ocurrio un error al crear el area (Nombres repetidos)", "Creacion fallida", JOptionPane.ERROR_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(this, "Ocurrio un error al crear el area", "Creacion fallida", JOptionPane.ERROR_MESSAGE);
                }
            }
            // this.setVisible(false);t
        }
    }//GEN-LAST:event_BCrearAreaActionPerformed

    private void CTNombreAreaNuevaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CTNombreAreaNuevaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_CTNombreAreaNuevaActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new InterfazCrearArea().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BCrearArea;
    private javax.swing.JTextField CTDescripcionArea;
    private javax.swing.JTextField CTNombreAreaNueva;
    private javax.swing.JTextField CTSalidaArea;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    // End of variables declaration//GEN-END:variables
}
